package com.edt.ichannel.api.service.impl;

import cc.edt.iceutils2.string.IceStringUtils;
import com.alibaba.fastjson.JSONObject;
import com.edt.ichannel.api.common.constant.CommonConstant;
import com.edt.ichannel.api.common.entity.ActionResult;
import com.edt.ichannel.api.entiy.local.ConsumeLog;
import com.edt.ichannel.api.entiy.local.WebApiEntity;
import com.edt.ichannel.api.service.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class CollectServiceImpl implements CollectService{
    private Logger logger = LogManager.getLogger(CollectService.class);
    @Resource
    private ValidateService validateService;
    @Resource
    private ConvertDataService convertDataService;

    @Resource
    private ConsumeLogService consumeLogService;

    @Resource
    private InterfaceLogService interfaceLogService;


    @Override
    public ActionResult saveConsume(String collectStr) {
        logger.info("接口接收参数："+ collectStr);
        ActionResult result = new ActionResult();
        if(IceStringUtils.isBlank(collectStr)){
            result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
            result.setMessage(CommonConstant.ACTIONRESULT_MESSAGE_NULL);
        }else{
            WebApiEntity entity = null;
            try{
               entity = JSONObject.parseObject(collectStr,WebApiEntity.class);
            }catch (Exception e){
                logger.info("--------json格式有误--------------");
                e.printStackTrace();
                result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
                result.setMessage(CommonConstant.ACTIONRESULT_MESSAGE_FORMATEERROR);
            }
            if(entity != null){
                try{
                    result = validateService.validateConsumeData(entity);
                    if(result.isSuccess()){
                        ConsumeLog consumeLog = convertDataService.convertDataToConsume(entity);
                        consumeLogService.saveConsumeLog(consumeLog);
//                        trafficService.exchangeAsync(entity.getPhone(),entity.getFlowvalue(),entity.getMechanismsid());
                    }
                }catch (Exception e){
                    logger.info("--------流程有误--------------");
                    result.setSuccess(CommonConstant.ACTIONRESULT_FAIL);
                    result.setMessage(CommonConstant.ACTIONRESULT_MESSAGE_SERVICEERROR);
                }

            }
        }
        interfaceLogService.addLog(result,collectStr,"/collect/consume",CommonConstant.INTERFACE_TYPE_COLLECT_CONSUME);
        return result;
    }

    @Override
    public ActionResult validateRequestMethod(String method) {
        ActionResult actionResult = new ActionResult();
        if(!CommonConstant.REQUEST_METHOD_POST.equals(method)){
            actionResult.setSuccess(false);
            actionResult.setMessage("只接受post请求");
        }else{
            actionResult.setSuccess(true);
        }
        return actionResult;
    }
}
